let fs = require("fs");
var path = require('path');
let nodeNameArr = [];

function readFileList(dir, filesList = [], findString) {
    let files = fs.readdirSync(dir, "utf-8");

    files = files.filter((file) => {
        return !file.endsWith(".meta");
    })
    files.forEach((item, index) => {
        var fullPath = path.join(dir, item);
        const stat = fs.statSync(fullPath);
        if (stat.isDirectory()) {
            readFileList(path.join(dir, item), filesList);  //递归读取文件
        } else {
            nodeNameArr.push(item.split('.')[0]);
        }
    });

    for (let i = 0; i < nodeNameArr.length; i++) {
        let one = nodeNameArr[i];
        let name = one.split(".")[0];
        if (i == nodeNameArr.length - 1) {
            findString += `\"${name}\"` + ";";
        } else {
            findString += `\"${name}\"` + " | ";
        }
        if ((i + 1) % 6 == 0) {
            findString += "\n\t";
        }
    }

    return findString;
}

//动态生成BgmName
let sourcesPath1 = "./assets/resources";
let targetFile1 = "./assets/script/framework/uiMoudle/UIManager.ts";
let findString1 = "type AssetName = ";
let pattern = new RegExp(`${findString1}[^;]*;`, "m");
let returnStr = readFileList(sourcesPath1, [], findString1);

// 替换
let content = fs.readFileSync(targetFile1, "utf-8");
content = content.replace(pattern, returnStr);
fs.writeFileSync(targetFile1, content);